﻿

using KinonekoSoftware.UI;

#if WINDOWS

using System.Windows.Media;
#else

using Avalonia.Media;

#endif

namespace KinonekoSoftware.Studio.Pages.Tools.Prompts.Birthday
{
    public class BirthdayPromptsViewModel : TabViewModelCore
    {
        private static readonly SolidColorBrush Brush_January   = MediaFactory.ToSolidColorBrush("#bd7651");
        private static readonly SolidColorBrush Brush_February  = MediaFactory.ToSolidColorBrush("#b593be");
        private static readonly SolidColorBrush Brush_March     = MediaFactory.ToSolidColorBrush("#b6e2da");
        private static readonly SolidColorBrush Brush_April     = MediaFactory.ToSolidColorBrush("#df4952");
        private static readonly SolidColorBrush Brush_May       = MediaFactory.ToSolidColorBrush("#7ab467");
        private static readonly SolidColorBrush Brush_June      = MediaFactory.ToSolidColorBrush("#fdd757");
        private static readonly SolidColorBrush Brush_July      = MediaFactory.ToSolidColorBrush("#e5afb6");
        private static readonly SolidColorBrush Brush_August    = MediaFactory.ToSolidColorBrush("#f18037");
        private static readonly SolidColorBrush Brush_September = MediaFactory.ToSolidColorBrush("#5f6ead");
        private static readonly SolidColorBrush Brush_October   = MediaFactory.ToSolidColorBrush("#98b3d3");
        private static readonly SolidColorBrush Brush_November  = MediaFactory.ToSolidColorBrush("#c04964");
        private static readonly SolidColorBrush Brush_December  = MediaFactory.ToSolidColorBrush("#127f91");
        private static readonly SolidColorBrush Brush_Black     = MediaFactory.ToSolidColorBrush(
                                                                                                 MediaFactory.ToColor(
                                                                                                                      Xplat.Theme.Colors[BrushKey.BackgroundLevel3]));
        private static readonly SolidColorBrush Brush_White = MediaFactory.ToSolidColorBrush(
                                                                                             MediaFactory.ToColor(
                                                                                                                  Xplat.Theme.Colors[BrushKey.ForegroundLevel1]));

        private readonly CompositeEngine          _engine_misc;
        private readonly InstanceCacheEngine _engine_cache;

        private int _version;

        public BirthdayPromptsViewModel()
        {
            Platform.GetEngine<CompositeEngine, InstanceCacheEngine>(out _, out _engine_misc, out _engine_cache);
            Birthdays = new ObservableCollection<BirthdayPromptHint>();
        }

        private void Initialized()
        {
            var birthdays = _engine_misc.GetBirthdays();

            //
            //
            Birthdays.Clear();

            //
            //
            foreach (var birthday in birthdays)
            {
                var character = _engine_cache.Get(birthday.Id);
                Birthdays.Add(new BirthdayPromptHint(birthday, character));
            }

            _version = _engine_misc.Version;
        }

        protected override void OnStart()
        {
            Initialized();
            base.OnStart();
        }

        protected override void OnResume()
        {
            if (_version != _engine_misc.Version)
            {
                Initialized();
            }
            base.OnResume();
        }

        public static SolidColorBrush GetBackground(int month)
        {
            return month switch
            {
                1  => Brush_January,
                2  => Brush_February,
                3  => Brush_March,
                4  => Brush_April,
                5  => Brush_May,
                6  => Brush_June,
                7  => Brush_July,
                8  => Brush_August,
                9  => Brush_September,
                10 => Brush_October,
                11 => Brush_November,
                _  => Brush_December,
            };
        }

        public static SolidColorBrush GetForeground(int month)
        {
            return month switch
            {
                3 => Brush_Black,
                6 => Brush_Black,
                _ => Brush_White,
            };
        }

        public static string GetMonthName(int month)
        {
            return month switch
            {
                1  => "January",
                2  => "February",
                3  => "March",
                4  => "April",
                5  => "May",
                6  => "June",
                7  => "July",
                8  => "August",
                9  => "September",
                10 => "October",
                11 => "November",
                _  => "December",
            };
        }


        public static string GetMonthShortName(int month)
        {
            return month switch
            {
                1  => "Jan",
                2  => "Feb",
                3  => "Mar",
                4  => "Apr",
                5  => "May",
                6  => "Jun",
                7  => "Jul",
                8  => "Aug",
                9  => "Sep",
                10 => "Oct",
                11 => "Nov",
                _  => "Dec",
            };
        }

        public string CurrentMonth
        {
            get
            {
                return LanguageManager.Language switch
                {
                    LanguageArea.English => GetMonthShortName(DateTime.Now.Month),
                    _                    => $"{DateTime.Now.Month}月",
                };
            }
        }

        public string CurrentDay
        {
            get
            {
                return LanguageManager.Language switch
                {
                    LanguageArea.English => $"{DateTime.Now.Day}",
                    _                    => $"{DateTime.Now.Day}日",
                };
            }
        }
        
        public Brush CurrentBackground => GetBackground(DateTime.Now.Month);
        public Brush CurrentForeground => GetForeground(DateTime.Now.Month);

        public ObservableCollection<BirthdayPromptHint> Birthdays { get; }
    }
}